/*
Dijjer detection
version 0.2 - 2004-12-21
written by duck in 2004 and released into the public domain.

Usage
-----
Load this script in the HTML head tag:
<script type="text/javascript" src="dijjer.js"></script>

Create a dijjer url:
<a href="http://dijjer.org/get/http://the.url/" onClick="javascript:return dijjer(this);">
my link</a>

*/

// Reasonable defaults
var dijjerUrl = 'http://127.0.0.1:9115/';
var dijjerGifUrl = dijjerUrl + 'graph.gif';
var dijjerGifWidth = 256;
var dijjerInterval = 500;
var dijjerImage = new Image();

/**
 * Prepare the dijjer test
 * @param e the calling object (use: this)
 * @return false when the url is okay (so that the browser doesn't follow the url itself)
 *         or true when the url argument isn't detected.
 */
function dijjer(e) {
    var url = e.href;
    var i = url.indexOf("get/");
    if (i != -1) {
        dijjerImage.src = dijjerGifUrl;
        setTimeout("checkDijjer('"+url+"');", dijjerInterval);
        return false;
    } else {
        return true;
    }
}

/**
 * Check if the test image was properly loaded and redirect accordingly
 * @param url the original calling url
 */
function checkDijjer(url) {
    if(typeof dijjerImage.width != "undefined") {
        if(dijjerImage.width == dijjerGifWidth) {
            // dijjer found
            var i = url.indexOf("get/");
            var target = url.substring(i + 4);
            // TODO append d.getTime() to prevent caching
            // var d = new Date();
            // redirect to the dijjer url
            location.href = dijjerUrl + target;
        } else {
            // dijjer not found, redirect to the original url
            location.href = url;
        }
    } else {
        // no width property, redirect to the original url
        location.href = url;
    }
}
